package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.management.RuntimeErrorException;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Brand;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.RepertoryBrand;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VBrand;
import com.jxc.service.BrandServiceI;
//import com.sun.tools.internal.ws.wsdl.document.jaxws.Exception;

@Service("brandService")
public class BrandServiceImpl implements BrandServiceI {

	@Autowired
	private BaseDaoI<Brand> brandDao;
	@Autowired
	private BaseDaoI<RepertoryBrand> repertoryBrandDao;

	@Override
	public List<VBrand> combobox(VBrand vbrand) {
		return this.changeModel(this.find(vbrand));
	}

	@Override
	public DataGrid datagrid(VBrand vbrand) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vbrand)));
		dg.setTotal(this.total(vbrand));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Brand brand = brandDao.get(Brand.class, Long.parseLong(id
							.trim()));
					if (brand != null) {
						brand.setIsDelete(true);
						brandDao.update(brand);
					}
				}
			}
		}
	}

	@Override
	public void save(VBrand vbrand) {
		String hql = "from Brand t where t.name=? and t.repertory.id=? and t.isDelete=?";
		List<Object> values = new ArrayList<Object>();
		values.add(vbrand.getName());
		values.add(vbrand.getRid());
		values.add(false);
		Brand brand = brandDao.get(hql, values);
		if (brand != null) {
			throw new RuntimeException();
		} else {
			brand = new Brand();
			BeanUtils.copyProperties(vbrand, brand);
			if (vbrand.getRid() != null) {
				brand.setRepertory(new Repertory(vbrand.getRid()));
			}
			if (vbrand.getUid() != null) {
				brand.setUser(new User(vbrand.getUid()));
			}
			brand.setIsDelete(false);
			this.brandDao.save(brand);
		}
	}

	@Override
	public void update(VBrand vbrand) {
		Brand brand = brandDao.get(Brand.class, vbrand.getId());
		BeanUtils.copyProperties(vbrand, brand,
				new String[] { "id", "isDelete" });
		if (vbrand.getUid() != null) {
			brand.setUser(new User(vbrand.getUid()));
		}
		if (vbrand.getRid() != null) {
			Repertory repertory = new Repertory();
			repertory.setId(vbrand.getRid());
			brand.setRepertory(repertory);
		}
		this.brandDao.update(brand);
	}

	private List<VBrand> changeModel(List<Brand> brands) {
		List<VBrand> vbrands = new ArrayList<VBrand>();
		if (brands != null && brands.size() > 0) {
			for (Brand brand : brands) {
				VBrand vbrand = new VBrand();
				BeanUtils.copyProperties(brand, vbrand);
				if (brand.getUser() != null) {
					User user = brand.getUser();
					vbrand.setUid(user.getId());
					vbrand.setUname(user.getUsername());
				}
				if(brand.getRepertory()!=null){
					vbrand.setRid(brand.getRepertory().getId());
					vbrand.setRname(brand.getRepertory().getName());
				}
				vbrands.add(vbrand);
			}
		}
		return vbrands;
	}

	private List<Brand> find(VBrand vbrand) {
		String hql = "from Brand t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vbrand, hql, values);

		if (vbrand.getSort() != null && vbrand.getOrder() != null) {
			hql += " order by " + vbrand.getSort() + " " + vbrand.getOrder();
		}
		return brandDao.find(hql, values, vbrand.getPage(), vbrand.getRows());
	}

	private Long total(VBrand vbrand) {
		String hql = "select count(*) from Brand t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vbrand, hql, values);
		return brandDao.count(hql, values);
	}

	private String addWhere(VBrand vbrand, String hql, List<Object> values) {
		if (vbrand.getName() != null && !vbrand.getName().trim().equals("")) {
			hql += " and t.name like ? ";
			values.add("%%" + vbrand.getName().trim() + "%%");
		}
		if (vbrand.getUid() != null) {
			hql += " and t.user.id = ?";
			values.add(vbrand.getUid());
		}
		if (vbrand.getRid() != null) {
			hql += " and t.repertory.id = ?";
			values.add(vbrand.getRid());
		}
		hql += " and t.isDelete = 0 ";
		return hql;
	}

}
